<?php

///INCLUDE ALL CORES
include_once("core/Controller.php");
include_once("core/Model.php");

function __autoload($className) {
	if (substr($className , strlen($className) - strlen('Controller'), strlen('Controller')) == "Controller") {
		$name = substr($className , 0 , strlen($className) - strlen('Controller'));
		$name = strtolower($name);
		include_once("controllers/" . $name . ".php");
	}
	else if (substr($className , strlen($className) - strlen('Model'), strlen('Model')) == "Model") {
		$name = substr($className , 0 , strlen($className) - strlen('Model'));
		$name = strtolower($name);
		include_once("models/" . $name . ".php");
	}
	else if (substr($className , strlen($className) - strlen('View'), strlen('View')) == "View") {
		$name = substr($className , 0 , strlen($className) - strlen('View'));
		$name = strtolower($name);
		include_once("views/" . $name . ".php");
	}
}

//mysql_connect("localhost" , "progin" , "progin");
//mysql_select_db("progin_215_13508040");
mysql_connect("localhost" , "root" , "");
mysql_select_db("foto_db");


///PARSE REQUEST
$requestedPath = $_SERVER['PATH_INFO'];
if ($requestedPath == "") $requestedPath = "/home/index";
$tokens = array();
$tokens = explode("/" , $requestedPath);


///INCLUDE CONTROLLERS AND CALL IT
//include_once("controllers/" . $tokens[1] . ".php");
$controllerName = ucfirst($tokens[1]) . "Controller";

$controller = new $controllerName();


///Settings global vars && configs
$GLOBALS['controller'] = $controller;
$GLOBALS['base_path'] = Controller::$PATH;

if (isset($tokens[2]))
	call_user_func_array(array($controller , $tokens[2]) , array_slice($tokens , 3 , count($tokens) - 3));
else
	call_user_func(array($controller , "index") , null);

?>
